package searchEngine;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SearchEngineServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp){
		try {
			String searchExpr = req.getParameter("searchExpr");
			String URL = req.getParameter("url");
			for (StringBuffer s1: SearchEngine.search(searchExpr, URL))
			{
				resp.getWriter().println(s1);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp){
		
		try {
			if(req.getCharacterEncoding() == null)
				   req.setCharacterEncoding("ISO-8859-1");
			String searchExpr = req.getParameter("searchExpr");
			String URL = req.getParameter("url");
			Iterator iterator = SearchEngine.search(searchExpr, URL).iterator(); 
			 while(iterator.hasNext()){
				resp.getWriter().println(iterator.next());
			 }
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
